Welcome back to give our players the ability to open the locked door inside of our basement.
We're going to have to give them a key, right?
However, we don't want to make getting this key very easy.
Instead, we want to have multiple different positions around the map that we are going to randomly
place our key model at.
That can be clicked to obtain the key to open the breaker room door.
This way, players are forced to explore and venture around our basement map to look for this key,
while also being chased by our Squidward AI monster.
So, inside of server storage and our other stuff folder, we have a whole bunch of different positions
around the map for our key.
So as you can see, there is a position we have on a crate hidden back here.
We have one and another spot behind our spam shelf.
We have another spot and we are in pitch darkness.
It is, uh, I believe this one's on the floor hidden behind some crates.
So there's a lot of different hidden positions on the map that we're going to place this key at.
We're going to take this breaker room key giver, which has a click part inside of it.
And when that is clicked, we're going to give the breaker room key tool to that particular player.
So inside of server script service we can open up our services and open up the door key service.
And then we're also going to have to open up our game communication enum for the server.
Inside of the door key service we'll need two services.
One is going to be server storage.
And the other service is going to be server script service.
We can create the variable to represent this module script.
So door key service is equal to a table.
And we will return that at the end of our script.
And then from this point we can make a reference to a few events.
One is going to be the game Comms event or the Bindable, so we'll call it Game Comms Bindable which
is in server storage, dot events, dot Bindesboll's dot game communication.
Because we're going to have a script tell us when we should place the key randomly on the map.
We're not going to do it instantly, but we're going to wait until we are told to do it.
So we can also get our game comms enum and require that from the server script service, dot server,
dot modules, dot enums, dot game communication.
And then we're also going to get our anti-cheat module script.
So we can require that from server script service, dot server dot modules, dot anti-cheat.
We're going to get that folder that stores all of the different possible key positions.
So we can call it key positions is equal to server storage dot other stuff dot key positions.
And we'll get all of the children inside of that folder.
We'll get the key tool itself which is in server storage, dot tools.
And it's called the breaker room key.
So we'll grab that.
And then we're also going to get the giver for the tool or the thing that we need to click so we can
call it key giver.
And that's in server storage dot other stuff dot breaker room key giver.
And then we're also going to need a random data type.
Now we can go ahead and create a function for spawning the key somewhere.
We would pass a part that represents the position, and then we would have to do the same math that
we did inside of our money service, where we're going to have to adjust the key frame of our key giver
to be flush with the surface of wherever this part is at.
We're also going to need a function to listen for when our key giver is clicked, so we can call on
clicked.
And this is where we're also going to use our anti-cheat to make sure the player didn't grab this key
from like across the map.
So to this function, we would have to pass the click part.
And the player that clicked our click detector.
And then we can also have a function to listen to our game Combinable.
So on game comms event and this will get passed in action.
And all we'll need for this service is an initialize function.
So door key service init.
And all we're going to do is listen to our game comms Bindable event and connect our on game comms event
function.
So now we can go ahead and go to our Game Communication enum and create a section just for our door
key service so we can do door key service.
And the only action that we're going to need in here is to set up our key giver.
So now we can check inside of our on game comms event function if our action is equal to the game comms
enum dot, get the door key service and listen to the setup key giver action.
If it is, then what we're going to do is we're going to generate a random number using RNG next integer
from one to the max number of key positions.
So we're just going to pick a random position inside of our table.
And then what we could do is we could have our spawn key function or spawn key giver.
When it places it on the map, it can return back to us the actual instance or model, and then we'll
refer to that model and listen for when it is clicked, so we can have this return back to us a model.
And we could get that model which would be the key giver.
And we can call this key giver on map.
And that's going to be equal to our spawn key function.
And we would pass from our key positions table whatever part is at this particular index.
And after we get this key giver inside of it, there is a part called our click part.
And inside of the click part we have our click detector.
And we're going to listen for when it is clicked.
And we can connect a lambda function to it, get the player.
And then we would call our on clicked function.
We would pass the key giver dot click part and then the player itself.
So to spawn our key giver on the map, what we need to do is we need to get a clone of our key giver.
So we'll clone that.
And then we're going to use the pivot two function on this model to change the key frame.
And the key frame is going to be a key frame dot new.
And we're going to get the part dot position.
And you remember we need to subtract it by vector three dot new zero on the x.
And then for the y we need to get the size of this part on the y axis divided by two.
So we're flush with the surface.
And then after that we're going to add a vector three dot new zero on the x axis.
But on the y axis.
This time we have to refer to our clone and get the extents size on the y axis and divide that by two.
So we get the proper position that we need to set for our part to be flush with the surface and zero
on the z axis.
So once we get this C frame here, then we can go ahead and rotate our key randomly as well.
So we'll multiply this by a C frame dot angles or add a keyframe dot angles.
We don't care about the x axis, but we do want to rotate it randomly around the y axis.
So we can use our RNG next integer anywhere from -360 to 360 degrees, and then zero on the z axis.
And that's all we're going to need to do for our pivot two function.
And it looks like I accidentally put this zero in the wrong spot.
We have to move it outside of our Math.round function, so we'll pass zero right here.
So we get zero on the x random rotation here on the y, and then zero rotation on the z.
And then once we do that we're going to set the clone dot parent equal to the workspace dot Krusty Krab.
And we'll just put it in the basement.
And then we can go ahead and return our caregiver clone.
And then once we get our key giver clone return back to here, we're going to listen for when it gets
clicked and we're going to call our onClick function.
What do you want to do when this function gets called?
Well, first we want to check if the player who clicked the click part cheated or not.
So we're going to get cheated and the message from our anti-cheat.
And we're going to check the player distance to our object.
We'll pass the player, we'll pass the click part.
And then the distance is going to be the click part dot click detector.
Dot max activation distance.
If this player cheated, then we're going to kick them and give them a message.
Returned back from our anti-cheat, and then we'll just return out of this function.
Otherwise, we're going to check if the player already has the key in their inventory or they're already
holding the key.
So we can call a variable key on player, and we'll check the player's backpack and find first child
and see if they have a breaker room key.
Or we can check the player's character and find first child breaker room key.
So if this player has a key on them, then we're just going to return as well.
Otherwise we can go ahead and get a clone of our key.
And then we can set the parent of our clone equal to the player dot backpack.
Perfect.
So now this means we can go ahead and open up our game sections module script.
And when we go to start the second section of our game, this is when we could go ahead and set up the
key giver or place it randomly in our basement.
So after we have teleported all of the players into the basement and we've done all of the different
setup or whatever, we can actually just insert it anywhere inside of here.
So for example, we could put it right here.
We'll refer to our game coms Bindable, and we'll fire and give the action in our game Coms enum on
the server dot door key service, and tell it to set up the key giver on the map.
Perfect.
So now if we go ahead and play and wait till we get to section two, we should have our key giver randomly
on the map somewhere.
So let's go ahead and test it out.
All right, so now that our basement section is initialized, we can go ahead and check to see if we
got that key giver inside of the basement folder.
And there it is our breaker room key giver.
And it's all the way over there.
So we can go ahead and grab our player here and try to migrate him over to wherever our key giver is
at.
So we'll go ahead and take a look again to see where it is exactly.
So it's right over there.
So we'll keep on moving our character to roughly wherever its position is at.
I believe it's inside of here somewhere.
And let's see if we can find it.
There it is.
So it appeared on top of this barrel.
And as you can see inside of our key giver, we also have a little point light in there.
It's very dim, but it's bright enough so you can actually see the key and it slightly illuminates its
surroundings.
So it's not impossible to see it in the dark.
But now that we have this key giver, if we go back to the client here and we go ahead and grab it,
unfortunately it doesn't look like we're actually able to get anything out of our key giver.
So let's go ahead and figure out why.
All right.
And the reason why we weren't getting anything out of our key giver is because we have to change the
variable reference here.
This is thinking that we're referring to our global or basically our key giver right here, instead
of the key giver or the cloned key giver that gets returned from our function.
So we actually need to copy this and make that reference here and also make that reference here.
That's why it wasn't working, but now should be working.
So let's go ahead and try it out again okay.
This time we can go ahead and check where our our breaker key giver spawned.
So if we go into the Krusty Krab into the basement, it's all the way it seems over here.
And it looks like we're going to have to actually, let's go ahead and copy the position and let's go
ahead and move our player to that position.
So if we close that out, go to our player and move the root part to this position.
And then we can go back to the client.
And here is our key line on the ground.
As you can see you can kind of see it glowing here.
But we can go ahead and click it.
And there we go.
We get our break room key.
So now we're going to go ahead and have to find our way to the door to go ahead and, you know, open
the breaker room so we can figure out where we are.
Exactly.
That would help, but I can't really see that well.
But I think we are getting close.
Yep.
Should be right back here.
Here we go.
So if we interact with the door, we require the key breaker room key.
So let's pull it out and open the door.
And there we go.
Now we are able to open the door and we've unlocked it with our key.
And now the door is locked.
Perfect.
All right, so our door key service is working great.
We're making excellent progress so far on this project.
So let's keep it going and I'll see you in the next lecture.